let joblistModule = (function () {
  let $jobName = $(".jobName"),
    $jobDesc = $('.jobDesc'),
    $inpBox = $('.inpBox'),
    $inpCheckbox = $inpBox.find('input[type=checkbox]'),
    $userhandle = $('#userhandle'),
    $departhandle = $('#departhandle'),
    $jobhandle = $('#jobhandle'),
    $departcustomer = $('#departcustomer'),
    $customerall = $('#customerall'),
    $resetpassword = $('#resetpassword'),
    $submit = $('.submit');

  let jobId = null;
  let power = '';
  let arr = [];
  let prop = '';

  async function bindJobInfo() {
    let result = await axios.get("/job/info", { params: { jobId } });
    let { desc,
      id,
      name, power } = result.data;
    // 编辑
    // 有bug，需要用正则来匹配
    if (power === 'userhandle|departhandle|jobhandle|customerall') {
      $userhandle.prop('checked', true);
      $departhandle.prop('checked', true);
      $jobhandle.prop('checked', true);
      $departcustomer.prop('checked', true);
      $customerall.prop('checked', true);
      $resetpassword.prop('checked', true);
      $submit.prop('checked', true);
    } else if (power === 'customerall') {
      $departcustomer.prop('checked', true);
      $customerall.prop('checked', true);
    } else if (power === 'customermy') {
      $departcustomer.prop('checked', true);
    } else if (power === 'userhandle|departhandle|jobhandle') {
      $userhandle.prop('checked', true);
      $departhandle.prop('checked', true);
      $jobhandle.prop('checked', true);
    } else if (power === 'userhandle') {
      $userhandle.prop('checked', true);
    }
    $jobName.val(name);
    $jobDesc.val(desc);
    // jobId = null;
    return;
  }
  // 监控复选按钮，获取权限值
  function getPower() {
    Array.prototype.forEach.call($inpCheckbox, item => {
      item.onchange = ev => {
        let flag = ev.target.checked;
        prop = ev.target.getAttribute("value");
        if (flag) {
          !arr.includes(prop) ? arr.push(prop) : null;
        } else {
          arr.includes(prop) ? arr = arr.filter(item => item !== prop) : null;
        }
      }
    })
  }
  // 点击事件
  function addHandle() {
    // 新增 name=xxx&desc=xxx&power=xxx|xxx
    getPower();
    $submit.click(async _ => {
      getPower();
      power = arr.join("|");
      let params = {
        name: $jobName.val().trim(),
        desc: $jobDesc.val().trim(),
        power
      };
      if (params.name.length <= 0 || params.desc.length <= 0 || params.power.length <= 0) {
        alert("职务名称|职务描述|拥有权限都为必填项，请填写！");
        return;
      }
      if (jobId) {
        console.log('??????????????')
        // 编辑‘ /job/update jobId=1&name=xxx&desc=xxx&power=xxx|xxx
        let params2 =
        {
          jobId,
          name: $jobName.val().trim(),
          desc: $jobDesc.val().trim(),
          power
        }
        console.log(params2);
        let result = await axios.post("/job/update", params2);
        console.log(result)
        window.location.href = 'joblist.html';
        return;
      }
      let result2 = await axios.post("/job/add", params);
      if (result2.code !== 0) {
        alert("新增失败，请重试");
        return;
      }
      window.location.href = 'joblist.html';
    })
  }
  return {
    init() {
      let params = window.location.href.queryURLParams();
      if (params.hasOwnProperty("jobId")) {
        jobId = params.jobId;
        bindJobInfo();
      }
      addHandle();
    }
  }
})();
joblistModule.init();